﻿using System;
using System.Collections.Generic;
using System.Reflection;

namespace MuYue.ORM
{
    /// <summary>
    /// 反射信息存放模型
    /// </summary>
    class DbTable
    {
        /// <summary>
        /// 构造函数
        /// 初始化列信息存储
        /// </summary>
        public DbTable() {
            Columns = new Dictionary<string, DbColumn>();
        }

        /// <summary>
        /// 数据表的名字 -- 实例对应的数据表  --默认为前缀类名 -- 可以通过属性类配置
        /// </summary>
        public string TableName { set; get; }

        /// <summary>
        /// 实体类的类型
        /// </summary>
        public Type TEntityType { set; get; }

        /// <summary>
        /// 字段信息
        /// 使用字段名来查询
        /// </summary>
        public Dictionary<string, DbColumn> Columns { private set; get; }

        /// <summary>
        /// 设置字段信息
        /// </summary>
        public void AddPropertyInfo(PropertyInfo info) {
            var column = new DbColumn(info);
            var name = (column.Key != null && column.Key.Alias != null) ? column.Key.Alias : info.Name;
            Columns.Add(name.ToLower(), column);
        }

        /// <summary>
        /// 获取字段信息
        /// </summary>
        /// <param name="columnName">列名</param>
        /// <param name="columnInfo">映射信息</param>
        /// <returns></returns>
        public bool GetDbColumn(string columnName, out DbColumn columnInfo) {
            return Columns.TryGetValue(columnName.ToLower(), out columnInfo);
        }

    }
}
